package com.dhl.fin.api.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.dhl.fin.api.common.dto.QueryDto;
import com.dhl.fin.api.common.service.CommonService;
import com.dhl.fin.api.common.util.DateUtil;
import com.dhl.fin.api.dao.MyStatementDao;
import com.dhl.fin.api.domain.BankStatement;
import com.dhl.fin.api.domain.BusinessRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author CuiJianbo
 * @since 2025/3/16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusinessRecordService extends CommonService<BusinessRecord> {

    @Autowired
    private MyStatementDao myStatementDao;


    public List<String> getBusinessRecordYearMonth(DateTime startTime,String familyCode) {
        return myStatementDao.queryBusinessRecordDateTime(DateUtil.getTime(startTime),familyCode);
    }

    public List<BusinessRecord> queryBusinessRecord(String yearmonth, Long bankAccountId) throws Exception {
        return select(QueryDto.builder()
                .available()
                .addWhere("bank_account_id = " + bankAccountId)
                .addWhere("DATE_FORMAT(create_time, '%Y-%m') = '" + yearmonth + "'")
                .build());
    }

    public void addBusinessRecord(BankStatement bankStatement) throws Exception {
        BusinessRecord businessRecord = new BusinessRecord();
        BeanUtil.copyProperties(bankStatement, businessRecord);
        businessRecord.setBusinessName(bankStatement.getTypeName());
        businessRecord.setBusinessBrief(String.format("存入%.0f元", bankStatement.getMoneyAmount()));
        businessRecord.setBankAccount(bankStatement.getBankAccount());
        businessRecord.setId(null);

        save(businessRecord);
    }

}
